let rootContext = {};

const HostConfig = {
  now: Date.now,
  supportsMutation: true,

  getRootHostContext: function (nextRootInstance) {
    return rootContext;
  },
  getChildHostContext: function (parentContext, fiberType, rootInstance) {
    let context = { type: fiberType };
    return context;
  },

  createInstance: function (
    type,
    newProps,
    rootContainerInstance,
    currentHostContext,
    workInProgress
  ) {
    const element = document.createElement(type);
    element.className = newProps.className || "";
    element.style = newProps.style;
    // ....
    // ....
    if (newProps.onClick) {
      element.addEventListener("click", newProps.onClick);
    }
    console.log(type);
    return element;
  },
  createTextInstance: function (
    newText,
    rootContainerInstance,
    currentHostContext,
    workInProgress
  ) {
    return document.createTextNode(newText);
  },

  clearContainer(container) {
    if (container.nodeType === 1) {
      container.textContent = "";
    } else if (container.nodeType === 9) {
      const body = container.body;
      if (body != null) {
        body.textContent = "";
      }
    }
  },

  shouldSetTextContent: function (type, nextProps) {
    return false;
  },

  finalizeInitialChildren: (
    instance,
    type,
    newProps,
    rootContainerInstance,
    currentHostContext
  ) => {
    return newProps.autofocus; //simply return true for experimenting
  },
  prepareForCommit: function (rootContainerInstance) {},
  resetAfterCommit: function (rootContainerInstance) {},
  commitMount: (domElement, type, newProps, fiberNode) => {
    domElement.focus();
  },
  prepareUpdate: function (
    instance,
    type,
    oldProps,
    newProps,
    rootContainerInstance,
    currentHostContext
  ) {
    return; //return nothing.
  },
  commitUpdate: function (
    instance,
    updatePayload,
    type,
    oldProps,
    newProps,
    finishedWork
  ) {
    debugger;
    return; //return nothing.
  },

  commitTextUpdate: function (textInstance, oldText, newText) {
    textInstance.nodeValue = newText;
  },
  resetTextContent: function (domElement) {},

  appendInitialChild: (parent, child) => {
    console.log("child", child);
    parent.appendChild(child);
  },

  appendChild: function (parentInstance, child) {
    console.log("child", child);
    parentInstance.appendChild(child);
  },
  insertBefore: (parentInstance, child, beforeChild) => {
    parentInstance.insertBefore(child, beforeChild);
  },
  removeChild: function (parentInstance, child) {
    parentInstance.removeChild(child);
  },

  appendChildToContainer: (parent, child) => {
    parent.appendChild(child);
  },
  insertInContainerBefore: function (container, child, beforeChild) {
    container.insertBefore(child, beforeChild);
  },
  removeChildFromContainer: function (container, child) {
    container.removeChild(child);
  },

  shouldDeprioritizeSubtree: function (type, nextProps) {
    return !!nextProps.hidden;
  },
};

module.exports = HostConfig;
